/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JTxtImportDialog.java
 *
 * Created on 2009-11-13, 9:29:23
 */

package cn.ekuma.epos.qkorder.dialog;

import cn.ekuma.epos.qkorder.JPanelOrder;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JCheckBox;
import javax.swing.SwingUtilities;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;

/**
 *
 * @author Administrator
 */
public class JTxtImportDialog extends javax.swing.JDialog {
    JPanelOrder orderEdit;
	private JCheckBox jCheckBox3;


    /** Creates new form JTicketChangeRateDialog 
     * @wbp.parser.constructor*/
    public JTxtImportDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

     /** Creates new form JProductLineKeyBoardEdit */
    private  JTxtImportDialog(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

     void init(JPanelOrder orderEdit) {
        this.orderEdit=orderEdit;
        jSplitType.addItem(new ComboBoxValEntry(",",","));
        jSplitType.addItem(new ComboBoxValEntry("\t","\\t"));
        jCheckBox1.setSelected(true);
        this.setVisible(true);
    }

    public static JTxtImportDialog showDialog(JPanelOrder orderEdit){
        Window window = SwingUtilities.getWindowAncestor(orderEdit);
        JTxtImportDialog myMsg;
        if (window instanceof Frame) {
            myMsg = new JTxtImportDialog((Frame) window, true);
        } else {
            myMsg = new JTxtImportDialog((Dialog) window, true);
        }
        myMsg.init(orderEdit);
        myMsg.applyComponentOrientation(orderEdit.getComponentOrientation());
        return myMsg;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel7 = new javax.swing.JPanel();
        m_jButtonOK = new javax.swing.JButton();
        m_jButtonCancel = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jSplitType = new javax.swing.JComboBox();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jInText = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jOutText = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 400));

        m_jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
        m_jButtonOK.setText(AppLocal.getIntString("Button.OK")); // NOI18N
        m_jButtonOK.setFocusPainted(false);
        m_jButtonOK.setFocusable(false);
        m_jButtonOK.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonOK.setRequestFocusEnabled(false);
        m_jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonOKActionPerformed(evt);
            }
        });

        m_jButtonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
        m_jButtonCancel.setText(AppLocal.getIntString("Button.Cancel")); // NOI18N
        m_jButtonCancel.setFocusPainted(false);
        m_jButtonCancel.setFocusable(false);
        m_jButtonCancel.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonCancel.setRequestFocusEnabled(false);
        m_jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(220, Short.MAX_VALUE)
                .addComponent(m_jButtonOK)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(m_jButtonCancel)
                .addGap(25, 25, 25))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(m_jButtonCancel, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                .addComponent(m_jButtonOK, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 32, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel7, java.awt.BorderLayout.PAGE_END);

        jPanel1.setMinimumSize(new java.awt.Dimension(427, 300));
        jPanel1.setPreferredSize(new java.awt.Dimension(427, 300));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel1.setText(AppLocal.getIntString("label.jtxtimport.splitTypeName")); // NOI18N

        buttonGroup1.add(jCheckBox1);
        jCheckBox1.setText(AppLocal.getIntString("check.txtimport.importSU")); // NOI18N

        buttonGroup1.add(jCheckBox2);
        jCheckBox2.setText(AppLocal.getIntString("check.txtimport.importSUP")); // NOI18N
        
        buttonGroup1.add(jCheckBox3);
        jCheckBox3.setText(AppLocal.getIntString("check.txtimport.importS")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2Layout.setHorizontalGroup(
        	jPanel2Layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(jPanel2Layout.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(jSplitType, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(jCheckBox3, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(jCheckBox1, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addComponent(jCheckBox2, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
        			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
        	jPanel2Layout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(Alignment.LEADING, jPanel2Layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel1)
        				.addComponent(jSplitType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jCheckBox1)
        				.addComponent(jCheckBox2)
        				.addComponent(jCheckBox3))
        			.addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel2.setLayout(jPanel2Layout);

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_START);

        jSplitPane1.setDividerLocation(550);
        jSplitPane1.setDividerSize(8);
        jSplitPane1.setLastDividerLocation(550);

        jScrollPane1.setViewportView(jInText);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jOutText.setEditable(false);
        jScrollPane2.setViewportView(jOutText);

        jSplitPane1.setRightComponent(jScrollPane2);

        jPanel1.add(jSplitPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void m_jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonOKActionPerformed
       incOutProduct();
}//GEN-LAST:event_m_jButtonOKActionPerformed

    private void m_jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonCancelActionPerformed
        dispose();
}//GEN-LAST:event_m_jButtonCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JTextPane jInText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextPane jOutText;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JComboBox jSplitType;
    private javax.swing.JButton m_jButtonCancel;
    private javax.swing.JButton m_jButtonOK;
    // End of variables declaration//GEN-END:variables

    private void incOutProduct() {
       List<InBean> inBeans=buildOutInList();
       String outText="";
       for(InBean bean:inBeans){
           if(orderEdit.incOutProduct(bean.getCode(), bean.getUnit(), bean.getPrice()))
               bean.setInputed(true);
           outText+=bean.toString();
       }
       jOutText.setText(outText);
    }

    private List<InBean> buildOutInList() {
        List<InBean> temp=new ArrayList<InBean>();
        String splitChar=(String) ((ComboBoxValEntry)jSplitType.getSelectedItem()).getKey();
        boolean priceInclude=jCheckBox2.isSelected();
        BufferedReader br=new BufferedReader(new StringReader(jInText.getText()));
         String line=null;
         InBean inBean=null;
        try {
            while ((line = br.readLine()) != null) {
                String[] bean = line.split(splitChar);
                inBean=new InBean(bean[0]);
                if(!jCheckBox3.isSelected()){
                try {
                    double unit = (Double) Formats.DOUBLE.parseValue(bean[1], 1);
                    inBean.setUnit(unit);
                } catch (BasicException ex) {   
                }
                }	
                if(priceInclude&&bean.length==3){
                    try {
                        double price = (Double) Formats.DOUBLE.parseValue(bean[2], 0);
                        inBean.setPrice(price);
                    } catch (BasicException ex) {
                        //new MessageInf(ex).show(this);
                    }
                }
                temp.add(inBean);
            }
        } catch (IOException ex) {
            new MessageInf(ex).show(this);
        }
        return temp;
    }


    class InBean{
        String code;
        double unit;
        double price;
        boolean inputed;
        public InBean( String code){
            this.code=code;
            unit=1;
            price=0;
            inputed=false;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public double getUnit() {
            return unit;
        }

        public void setUnit(double unit) {
            this.unit = unit;
        }

        public boolean isInputed() {
            return inputed;
        }

        public void setInputed(boolean inputed) {
            this.inputed = inputed;
        }

        @Override
        public String toString() {
            String srt=code+","+unit+","+price;
            if(!inputed){
                srt+="---->false";
            }
            srt+="\r\n";
            return srt;
        }
    }
}
